package com.ddxz.middleground.meta.controller;

import com.ddxz.middleground.meta.dto.MetaHeader;
import com.ddxz.middleground.meta.dto.object.ObjectPage;
import com.ddxz.middleground.meta.dto.object.RestObjectInfo;
import com.ddxz.middleground.meta.dto.object.TableObjectInfo;
import com.ddxz.middleground.meta.dto.object.TaskObjectInfo;
import com.ddxz.middleground.meta.service.ObjectService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.annotation.DdxzRequestParam;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 对象的基本操作，主要包含：对象清单的检索，对象详情的查看，对象显示名称的修改，对象删除等基本操作。
 * 此外还包含有对象注册内容，撤销注册等相关的操作。除数据管理员和数据操作员之外，其他用户都只能看到自己的对象
 * 对象发布、撤销发布。将对象发布到
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/8/21
 */

@RestController
@Api("对象管理")
public class ObjectController {
    @ApiOperation(value = "资源检索", consumes = "application/json", produces = "application/json",
            notes = "以分页的形式检索资源清单，仅查看自己的资源（数据管理员和数据操作员除外）")
    @RequestMapping(value = "/object/list", method = RequestMethod.GET)
    public DdxzResponse objectList(@DdxzRequestHeader MetaHeader header, @DdxzRequestParam ObjectPage page) {
        return service.objectSearch(header, page, 0);
    }

    @ApiOperation(value = "资产检索", consumes = "application/json", produces = "application/json",
            notes = "以分页的形式检索资产清单，仅查看自己的资源（数据管理员和数据操作员除外）")
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    public DdxzResponse productList(@DdxzRequestHeader MetaHeader header, @DdxzRequestParam ObjectPage page) {
        return service.objectSearch(header, page, 1);
    }

    @ApiOperation(value = "订阅检索", consumes = "application/json", produces = "application/json",
            notes = "以分页的形式检索资产清单，仅查看他人的资源")
    @RequestMapping(value = "/subscribe/list", method = RequestMethod.GET)
    public DdxzResponse subscribeList(@DdxzRequestHeader MetaHeader header, @DdxzRequestParam ObjectPage page) {
        return service.objectSearch(header, page, 2);
    }

    @ApiOperation(value = "资源详情", consumes = "application/json", produces = "application/json",
            notes = "查看资源的详细信息")
    @RequestMapping(value = "/object/{objectId}/detail", method = RequestMethod.GET)
    public DdxzResponse objectDetail(@DdxzRequestHeader MetaHeader header, @PathVariable("objectId") String objectId) {
        return service.objectDetail(header, objectId);
    }

    @ApiOperation(value = "表资源存储", consumes = "application/json", produces = "application/json",
            notes = "将数据表类型的资源存储到数据库中")
    @RequestMapping(value = "/table/save", method = RequestMethod.POST)
    public DdxzResponse objectSave(@DdxzRequestHeader MetaHeader header, @RequestBody TableObjectInfo tableInfo) {
        return service.objectSave(header, tableInfo);
    }

    @ApiOperation(value = "接口资源保存", consumes = "application/json", produces = "application/json",
            notes = "将接口类型的资源存储到数据库中")
    @RequestMapping(value = "/rest/save", method = RequestMethod.POST)
    public DdxzResponse objectSave(@DdxzRequestHeader MetaHeader header, @RequestBody RestObjectInfo apiInfo) {
        return service.objectSave(header, apiInfo);
    }

    @ApiOperation(value = "任务资源保存", consumes = "application/json", produces = "application/json",
            notes = "将任务类型的资源保存到数据库中")
    @RequestMapping(value = "/task/save", method = RequestMethod.POST)
    public DdxzResponse objectSave(@DdxzRequestHeader MetaHeader header, @RequestBody TaskObjectInfo taskInfo) {
        return service.objectSave(header, taskInfo);
    }

    @ApiOperation(value = "删除资源", consumes = "application/json", produces = "application/json",
            notes = "删除自己的资源，只有资源未注册时可以删除，已注册的资源不可删除")
    @RequestMapping(value = "/object/{objectId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse objectRemove(@DdxzRequestHeader MetaHeader header, @PathVariable("objectId") String objectId) {
        return service.objectRemove(header, objectId);
    }

    @ApiOperation(value = "注册目录", consumes = "application/json", produces = "application/json",
            notes = "将资产注册到一个或多个目录中")
    @RequestMapping(value = "/object/{objectId}/register", method = RequestMethod.POST)
    public DdxzResponse objectRegister(@DdxzRequestHeader MetaHeader header, @PathVariable("objectId") String objectId, @RequestBody List<String> dirIdList) {
        return service.objectRegister(header, objectId, dirIdList);
    }

    @ApiOperation(value = "撤销注册", consumes = "application/json", produces = "application/json",
            notes = "将资产从所有数据目录中移除")
    @RequestMapping(value = "/object/{objectId}/unregister", method = RequestMethod.POST)
    public DdxzResponse objectUnregister(@DdxzRequestHeader MetaHeader header, @PathVariable("objectId") String objectId) {
        return service.objectUnregister(header, objectId);
    }

    @ApiOperation(value = "对象发布", consumes = "application/json", produces = "application/json",
            notes = "将资产发布为可以订阅的资产（自己的资产直接完成订阅）")
    @RequestMapping(value = "/object/{objectId}/issue", method = RequestMethod.PUT)
    public DdxzResponse objectIssue(@DdxzRequestHeader MetaHeader header,
                                    @PathVariable("objectId") String objectId,
                                    @RequestParam(name = "permission") Integer permission) {
        return service.objectIssue(header, objectId, permission);
    }

    @ApiOperation(value = "撤销发布", consumes = "application/json", produces = "application/json",
            notes = "将已经发布的资产修改为未发布状态，（如果当前资产有人订阅则不允许撤销发布）")
    @RequestMapping(value = "/object/{objectId}/unissued", method = RequestMethod.PUT)
    public DdxzResponse objectUnissued(@DdxzRequestHeader MetaHeader header,
                                    @PathVariable("objectId") String objectId) {
        return service.objectUnissued(header, objectId);
    }

    @ApiOperation(value = "订阅者清单", consumes = "application/json", produces = "application/json",
            notes = "查看当前资源的所有订阅者")
    @RequestMapping(value = "/object/{objectId}/subscriber", method = RequestMethod.GET)
    public DdxzResponse objectSubscriber(@DdxzRequestHeader MetaHeader header, @PathVariable("objectId") String objectId) {
        return service.objectDetail(header, objectId);
    }

    private final ObjectService service;

    public ObjectController(ObjectService service) {
        this.service = service;
    }
}
